<?php
/**
 * Created by PhpStorm.
 * User: Happy
 * Date: 2016/6/2 0002
 * Time: 12:12
 */

class jquery_datatablePlugin  extends Plugin
{
   //显示数据示例
    public  function  show_demo(){

        $config=$this->config;
        $dataset=Model3('goods')->field('goods_id,goods_name,goods_jingle,store_name,goods_price')->limit(100)->select();
        // $colums=array('id','姓名','店铺id','密码','登陆次数'); //简单控制字段名称
        //控制显示隐藏和只读属性和显示模式
        $columns=array(
            array(
                'name'=>'选择',
                'field'=>'goods_id',
                'html'=>'<input data-key=\"{admin_id}\"  type=\"checkbox\"/>'
            ),
            array(
                'name'=>'商品名',
                'field'=>'goods_name',
            ),
            array(
                'name'=>'商品介绍',
                'field'=>'goods_jingle',
                'html'=>'<textarea>{goods_jingle}</textarea>'
            ),
            array(
                'name'=>'商店名',
                'field'=>'store_name',
                'mode'=>'read'  //只读模式不可以修改
            ),
            array(
                'name'=>'价格',
                'field'=>'goods_price',
            )
        );

        $tpl=$config['plugin_path'].'/tpl/demo.tpl';
        $tpl= $this->replace_url(file_get_contents($tpl));
        $t=new BlitzPhp();
        echo  $t->compile($tpl,array('data'=>json_encode($dataset),'mainkey'=>'admin_id','colums'=>json_encode($colums)));

    }

    //处理数据 主要是单引号，双引号的问题
    public  function   filter_data($data=array(),$type=0){//0代表数组，1 代表返回json
        foreach($data as $key=>$value){
            if( $value['html']){
                $html=$value['html'];
                $data[$key]['html']=addslashes(str_replace(PHP_EOL,'',$html)); //移除换行和双引号转义
                //  write($value[$key]['html']);
            }
        }
        if($type){
            return json_encode($data);
        }
    }

   //替换资源相对路径
    public function  replace_url($content){

        $plugin_name=basename($this->c('plugin_path'));
       return str_replace(array('./js/','./css/','./images','../../../resource/'),array("./plugins/$plugin_name/tpl/js/","./plugins/$plugin_name/tpl/css/","./plugins/$plugin_name/tpl/images",'./resource/'),$content);
    }

    //单条记录示例
    public  function  item_table_demo(){
        $config=$this->config;
    $data=array(
     'id'=>'23',
     'room_name'=>'高级客房',
     'room_size'=>24,
     'room_price'=>'127.0',
    );

     $detail=array(
       'room_name'=>array(
           'name'=>'名称'
          ),
         'room_size'=>array(
             'name'=>'房间大小',
             'html'=>'{room_size}平米'  // 代表键值指向的值
         ),
         'room_price'=>array(
             'name'=>'房间价格',
             'html'=>'{room_price}元'
         )
     );
        $tpl=$config['plugin_path'].'/tpl/item.tpl';
        $tpl= $this->replace_url(file_get_contents($tpl));
        $t=new BlitzPhp();
        echo  $t->compile($tpl,array('data'=>json_encode($data),'columns'=>json_encode($detail)));
    }




}